package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.Comic;
import com.qianfeng.qlm.handforireader.util.Constant;

import java.nio.channels.Pipe;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/25
 * Email:tadhg299@163.com
 */
public class ComicRecycleAdapter extends RecyclerView.Adapter<ComicRecycleAdapter.ViewHolder> {

    public static final String TAG = ComicRecycleAdapter.class.getSimpleName();

    private List<Comic> comics;
    private Context context;
    private RequestQueue requestQueue;
    private String flag;
    private View.OnClickListener listener;

    public ComicRecycleAdapter(List<Comic> comics, Context context , View.OnClickListener listener , String flag) {
        this.comics = comics;
        this.context = context;
        requestQueue = Volley.newRequestQueue(context);
        this.flag = flag;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_home_comic, parent, false);
        itemView.setTag(flag);
        itemView.setOnClickListener(listener);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Comic comic = comics.get(position);
        holder.comicLastIndex.setText("更新至第" + comic.getComic_last_orderidx() + "话");
        holder.comicName.setText(comic.getComic_name());
        holder.comicDesc.setText(comic.getComic_desc());

        long maxMemory = Runtime.getRuntime().maxMemory() / 8;
       /* final LruCache<String, Bitmap> lruCache = new LruCache<>((int)maxMemory);

        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache(){
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url,bitmap);
            }
        };

        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
        holder.comicPic.setDefaultImageResId(R.mipmap.homebg);
        holder.comicPic.setErrorImageResId(R.mipmap.homebg);
        holder.comicPic.setImageUrl(Constant.urlSuffix + comic.getComic_pic_240_320(),imageLoader);*/

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.homebg);
        bitmapUtils.display(holder.comicPic,Constant.URL_SUFFIX + comic.getComic_pic_240_320());

    }

    @Override
    public int getItemCount() {
        return comics.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView comicPic;
        private TextView comicLastIndex;
        private TextView comicName;
        private TextView comicDesc;

        public ViewHolder(View itemView) {
            super(itemView);

            comicPic = (ImageView)itemView.findViewById(R.id.comic_pic);
            comicLastIndex = (TextView)itemView.findViewById(R.id.comic_last_index);
            comicName = (TextView)itemView.findViewById(R.id.comic_name);
            comicDesc = (TextView)itemView.findViewById(R.id.comic_desc);
        }
    }

    public void addAll(List<Comic> list){
        comics.addAll(list);
        notifyDataSetChanged();
    }

    public Comic getItemData(int position){
        return comics.get(position);
    }
}
